package com.handsome.service.impl;

import com.github.pagehelper.PageHelper;
import com.handsome.dao.HsUserDefinedSqlMapper;
import com.handsome.dao.HsWidgetMapper;
import com.handsome.dto.BootstrapDataTableDto;
import com.handsome.entity.HsWidget;
import com.handsome.service.WidgetService;
import com.handsome.utils.Result;
import com.handsome.utils.ResultGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * WidgetServiceImpl
 *
 * @author Jiangda
 */
@Service
public class WidgetServiceImpl implements WidgetService {


    @Autowired
    private HsWidgetMapper hsWidgetMapper;

    @Autowired
    private HsUserDefinedSqlMapper hsUserDefinedSqlMapper;

    @Override
    public Result buildForm(HsWidget widget) {
        Integer row = hsWidgetMapper.insert(widget);
        if (row > 0) {
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("创建表单失败请稍后再试");
    }

    @Override
    public BootstrapDataTableDto getFormList() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int offset = Integer.parseInt(request.getParameter("offset"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String search = request.getParameter("search");
        BootstrapDataTableDto<HsWidget> tableDto = new BootstrapDataTableDto<>();
        int count = hsWidgetMapper.selectCount(new HsWidget());
        tableDto.setTotal(count);
        PageHelper.offsetPage(offset, limit);
        List<HsWidget> widgets = null;
        Example example = new Example(HsWidget.class);
        example.setOrderByClause("id desc");

        if (!StringUtils.isBlank(search)) {
            example.createCriteria().andLike("description", search).orLike("name", search).orLike("id", search);

        } else {
            example.createCriteria().andIsNotNull("id");
        }
        widgets = hsWidgetMapper.selectByExample(example);

        tableDto.setRows(widgets);
        return tableDto;
    }

    @Override
    public HsWidget getFormById(Integer id) {

        return hsWidgetMapper.selectByPrimaryKey(id);
    }

    @Override
    public HashMap<String, Object> buildGetEntitySql(String tableName, Integer id) {
        String[] strings = tableName.split("\\.");
        String table = strings[strings.length - 1].replace("Entity", "").replace("Hs", "hs");
        StringBuilder sb = new StringBuilder(table);
        String splitSymbol = "_";
        int temp = 0;
        if (!table.contains(splitSymbol)) {
            for (int i = 0; i < table.length(); i++) {
                if (Character.isUpperCase(table.charAt(i))) {
                    sb.insert(i + temp, splitSymbol);
                    temp += 1;
                }
            }
        }
        tableName = sb.toString().toLowerCase();

        return hsUserDefinedSqlMapper.buildGetEntitySql(tableName, id);
    }
}
